#include <my_header.h>
#include "../include/func.h"

/* Usage: 客户端  */


int main(int argc, char *argv[]){

    // ./client ../conf/client.conf
    ARGS_CHECK(argc, 2);
    char ip[20] = {0};
    char port[10] = {0};
    getClientConf(argv[1], ip, port);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sockfd, -1, "socket");
    // setsockopt
    int flag = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
    // addr
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(ip);
    addr.sin_port = htons(atoi(port));
    int ret = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    ERROR_CHECK(ret, -1, "connect");
    printf("connecting server...\n");
    // 用户登录
    login_client(sockfd);
    printf("login success!\n");

    // 反馈登录成功后，从标准输入中读取命令
    int exit_flag = 0;  // 0运行，-1退出
    while (1) {
        // 重复先发后收
        int tag = 0;
        char line[128] = "";
        cmd_t cmd;
        memset(&cmd, 0, sizeof(cmd_t));
        memset(line, 0, sizeof(line));
        read(STDIN_FILENO, line, sizeof(line));
        line[strlen(line) - 1] = '\0';
        // 检查退出命令
        if (!strcmp(line, "quit")) {
            // 检查退出标志
            exit_flag = -1;
            send(sockfd, &exit_flag, sizeof(int), 0);
            break;
        }

        // 非退出指令，格式化这行数据，并存入cmd
        init_cmd(line, &cmd);
        // 打印命令
        for (int i = 0; i<cmd.argc;i++) {
            printf("argv %d = %s\n",i, cmd.cmd[i]);
        }
        printf("successs read\n");
        // TLV协议，初始化小火车
        train_t train;
        memset(&train, 0, sizeof(train_t));
        if (cmd_check(&cmd, &tag) == 0){
            train.tag = tag;
        } else {
            printf("invalid cmd. Please input again.\n");
            continue;
        }
        send(sockfd, &train.tag, sizeof(int), 0);
        // 根据tag进行不同的发送行为 和 接收行为 
        memcpy(&train.data, &cmd, sizeof(cmd));
        train.data_length = sizeof(cmd);
        // 发送
        send(sockfd, &train.data_length, sizeof(int), 0);
        send(sockfd, &train.data, train.data_length, 0);

        // switch(tag)
        switch (tag) {
        case TAGCD:
            recv_cd(sockfd, &train);
            break;
        case TAGLS:
            recv_ls(sockfd, &train);
            break;
        case TAGPWD:
            recv_pwd(sockfd, &train);
            break;
        case TAGMKDIR:
            recv_mkdir(sockfd, &train);
            break;
        case TAGRMDIR:
            recv_rmdir(sockfd, &train);
            break;
        case TAGREMOVE:
            recv_rm(sockfd, &train);
            break;
        case TAGGETS:
            recv_gets(sockfd, &train, &cmd);
            break;
        case TAGPUTS:
            recv_puts(sockfd, &train, &cmd);
            break;
        default:
            break;
        }

        // if判断
        /* if (ptag == TAGPWD) { */
        /*     recv(sockfd, &train.data_length, sizeof(int), MSG_WAITALL); */
        /*     recv(sockfd, &train.data, train.data_length, MSG_WAITALL); */
        /*     printf("res = %s\n", train.data); */
        /* }else if (ptag == TAGLS){ */

        /* }else if (ptag == TAGCD) { */
        /*     recv(sockfd, &train.data_length, sizeof(int), MSG_WAITALL); */
        /*     recv(sockfd, &train.data, train.data_length, MSG_WAITALL); */
        /*     printf("res = %s\n", train.data); */

        /* } */

    }



    if (exit_flag == -1) {
        close(sockfd);
    }
    return 0;
}

